import { GameConfigData, GameData } from "../common/GameConfig";

const {ccclass, property} = cc._decorator;
export let GameUIs:GameUI;
@ccclass
export default class GameUI extends cc.Component {
    @property(cc.Node)
    nodeWalkContentTop:cc.Node = null;
    @property(cc.Node)
    nodeWalkContentBottom:cc.Node = null;
    @property(sp.SkeletonData)
    spPeopleArr:sp.SkeletonData[] = [];
    @property(cc.Node)
    nodeWalk:cc.Node = null;
    @property(sp.SkeletonData)
    spFishArr: sp.SkeletonData[] = [];
    @property(sp.SkeletonData)
    spFishArrBei:sp.SkeletonData[] = [];
    @property(cc.Node)
    nodeFishPeopleArr:cc.Node[] = [];

    onLoad () {
        GameUIs = this;
        this.initGameViewData();
    }

    initGameViewData(){
        for(let i = 0; i < GameData.fishData.length; i++){
            console.log(GameData.fishData[i])
            let itemData = GameData.fishData[i];
            let nodeItem = this.nodeFishPeopleArr[i].getComponent("nodeItem");
            let data = {};
            if(itemData.status === 2){
                data = {
                    etime:itemData.etime,
                    index:itemData.people,
                    type:itemData.fish_id,
                    sp: i <= 2?this.spFishArr[itemData.people]:this.spFishArrBei[itemData.people],
                    status:itemData.status
                }
            }
            else
            {
                data = itemData;
            }
            nodeItem.setItemData(data);
        }
    }

    setPeopleWalkAnimAndEndPosi(endPosi,callback,direction, fishType ){
        //随机获取钓鱼人的类型
        let type = ~~(Math.random()*this.spPeopleArr.length);
        let node = cc.instantiate(this.nodeWalk);
        node.active = true;
        let skeleton = node.getComponent(sp.Skeleton);
        skeleton.skeletonData = this.spPeopleArr[type];
        let size_width = cc.winSize.width / 2 + 80;
        let rand_dire = Math.random() > 0.5?size_width:-size_width;
        let nodePosi = null;
        let fishData = null;
        if(direction === "top")
        {
            //上面的位置
            nodePosi = this.nodeWalkContentTop.parent.convertToNodeSpaceAR(endPosi);
            this.nodeWalkContentTop.addChild(node)
            fishData = this.spFishArr[type];
        }
        else
        {
            //下面的位置
            nodePosi = this.nodeWalkContentBottom.parent.parent.convertToNodeSpaceAR(endPosi);
            this.nodeWalkContentBottom.addChild(node)
            fishData = this.spFishArrBei[type]
        }

        if(rand_dire > nodePosi.x)node.scaleX = -0.7;
        else node.scaleX = 0.7;

        node.setPosition(rand_dire,-28);
        skeleton.setAnimation(1,"animation",true)
        let time = Math.abs(rand_dire - nodePosi.x) > size_width /2?5:3;
        cc.tween(node)
            .to(time,{x:nodePosi.x})
            .call(()=>{
                let fishConfig = GameConfigData.fishTimeConfig[fishType - 1];
                let data = {
                    sp:fishData,
                    index:type,
                    etime:fishConfig.time,
                    type:fishConfig.id,
                    status:2
                }
                callback(data);
                node.active = false;
                node.removeFromParent();
                node.destroy();
            })
            .start()
    }
}
